﻿Imports System.Net
Imports System.IO
Imports System.Web
Imports Microsoft.Win32
Imports System.Security
Imports System.Text

Module UtilityMod

    Public Settings_ExcludeSites As String
    Public Settings_BlockSites As String
    Public Settings_BadKeywords As String
    Public Settings_StrictMode As String
    Public Settings_URLBlacklistMode As String

    Public AP_Server As String = "http://antiporn.felsoft.com.au/"

    Public AP_Username As String
    Public AP_Password As String

    Private Reg_UserKey As String = "AP01K"
    Private Reg_PassKey As String = "AP02K"

    Public Sub logRequest(ByVal user As String, ByVal pass As String, ByVal url As String)
        Dim m_url = AP_Server + "rpc_log_p.php"
        Dim resp = getWebPagePOST(m_url, "u=" + user + "&p=" + pass + "&c=" + getComputer() + "&url=" + HttpUtility.UrlEncode(url))
    End Sub

    Public Sub loadSettings(ByVal user As String, ByVal pass As String)
        Dim m_url = AP_Server + "rpc_settings_p.php"
        Dim resp = getWebPagePOST(m_url, "u=" + user + "&p=" + pass + "&c=" + getComputer())
        Dim array As String() = resp.Split("|")
        Settings_ExcludeSites = array(0)
        Settings_BlockSites = array(1)
        Settings_BadKeywords = array(2)
        Settings_StrictMode = array(3)
        Settings_URLBlacklistMode = array(4)
    End Sub

    Public Function getWebPagePOST(ByVal url As String, ByVal postData As String) As String
        Dim request As WebRequest = WebRequest.Create(url)
        request.Method = "POST"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentType = "application/x-www-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()
        Return responseFromServer
    End Function

    Public Function getWebPageGET(ByVal url As String) As String
        Dim request As WebRequest = WebRequest.Create(url)
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Return responseFromServer
    End Function

    Public Sub setAutoStartup()
        'sets everytime it starts to make it up todate
        Dim startPath As RegistryKey
        startPath =
        Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", True)
        If Not startPath Is Nothing Then
            startPath.SetValue("AntiPorn", Application.ExecutablePath)
            startPath.Close()
        End If
    End Sub

    Public Sub saveUserPass()
        Dim AntiPorn_Reg As RegistryKey
        AntiPorn_Reg =
        Registry.CurrentUser.OpenSubKey("SOFTWARE\\AntiPorn", True)

        If AntiPorn_Reg Is Nothing Then
            AntiPorn_Reg =
        Registry.CurrentUser.CreateSubKey("SOFTWARE\\AntiPorn")
        End If

        If Not AntiPorn_Reg Is Nothing Then
            AntiPorn_Reg.SetValue(Reg_UserKey, AP_Username)
            AntiPorn_Reg.SetValue(Reg_PassKey, AP_Password)
            AntiPorn_Reg.Close()
        End If
    End Sub

    Public Sub deleteUserPass()
        Dim AntiPorn_Reg As RegistryKey
        AntiPorn_Reg =
        Registry.CurrentUser.OpenSubKey("SOFTWARE\\AntiPorn", True)
        If Not AntiPorn_Reg Is Nothing Then
            AntiPorn_Reg.DeleteValue(Reg_UserKey)
            AntiPorn_Reg.DeleteValue(Reg_PassKey)
        End If
    End Sub

    Public Function getStoredUser() As String
        Dim AntiPorn_Reg As RegistryKey
        Dim user As String = Nothing
        AntiPorn_Reg =
        Registry.CurrentUser.OpenSubKey("SOFTWARE\\AntiPorn", False)
        If Not AntiPorn_Reg Is Nothing Then
            Dim user_str = AntiPorn_Reg.GetValue(Reg_UserKey)
            If Not user_str Is Nothing Then
                user = user_str
            End If
        End If
        Return user
    End Function

    Public Function getStoredPassword() As String
        Dim AntiPorn_Reg As RegistryKey
        Dim pass As String = Nothing
        AntiPorn_Reg =
        Registry.CurrentUser.OpenSubKey("SOFTWARE\\AntiPorn", False)
        If Not AntiPorn_Reg Is Nothing Then
            Dim pass_str = AntiPorn_Reg.GetValue(Reg_PassKey)
            If Not pass_str Is Nothing Then
                pass = pass_str
            End If
        End If
        Return pass
    End Function

    Public Function getComputer() As String
        Return HttpUtility.UrlEncode(System.Environment.MachineName)
    End Function

    Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New Cryptography.MD5CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
        bytesToHash = md5Obj.ComputeHash(bytesToHash)
        Dim strResult As String = ""
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
        Return strResult
    End Function

End Module
